﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.Core.Util;
using FSS.Core.Controls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using System.Xml;
using System.Xml.Xsl;
using IBatisNet.Common.Logging;
using FSS.BLL;

namespace SalesManagement.Modules.List
{

    public partial class Object : BasePage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Currency));
        public Object()
            : base(SCREENS.OBJECT)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            base.CheckPermissionControl();
            this.WindowEditor.SaveClick += new EventHandler(WindowEditor_SaveClick);
        }
        void WindowEditor_SaveClick(object sender, EventArgs e)
        {
            BindingObject();
        }
        [DirectMethod]
        public void DocumentReady()
        {
            BindingObject();
        }
        private void BindingObject()
        {
            ObjectEntity objectE = new ObjectEntity();
            ObjectService objectS = ObjectService.GetInstance();
            List<ObjectEntity> lstObject = (List<ObjectEntity>)objectS.LoadAll();
            if (lstObject != null)
            {
                storeMain.DataSource = lstObject;
                storeMain.DataBind();
            }
        }
        /// <summary>
        /// lấy id dòng được chọn
        /// </summary>
        /// <returns></returns>
        private int getIdRow()
        {
            RowSelectionModel sm = this.grdMain.SelectionModel.Primary as RowSelectionModel;
            SelectedRow row = new SelectedRow();
            row = sm.SelectedRow;
            if (row != null)
                return Int32.Parse(row.RecordID.ToString());
            else
                return -1;
        }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e)
        {
            int id = Int32.Parse(e.ExtraParams["id"]);
            string command = e.ExtraParams["command"];
            if (IsEdit && Constants.Edit.Equals(command))
            {
                this.WindowEditor.Show(id);
            }
            else if (IsDelete && Constants.Delete.Equals(command))
            {
                ObjectEntity objectE = new ObjectEntity();
                ObjectService objectS = ObjectService.GetInstance();
                objectE.ObjectId = id;
                objectS.Delete(objectE);
                BindingObject();
            }
        }
        protected void grdMain_OnAfterEdit(object sender, DirectEventArgs e)
        {
            try
            {
                bool IsActive = bool.Parse(e.ExtraParams["IsActive"]);
                bool IsCustomer = bool.Parse(e.ExtraParams["IsCustomer"]);
                bool IsProvider = bool.Parse(e.ExtraParams["IsProvider"]);
                int id = int.Parse(e.ExtraParams["ObjectId"]);
                ObjectEntity objectE = new ObjectEntity();
                ObjectService objectS = ObjectService.GetInstance();
                objectE.IsActive = IsActive;
                objectE.IsCustomer = IsCustomer;
                objectE.IsProvider = IsProvider;
                objectE.ObjectId = id;
                objectS.ObjectUpdateCheck(objectE);
            }
            catch (Exception ex)
            {
                X.Msg.Alert("Error", ex.Message).Show();
            }
        }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            this.WindowEditor.Show();
        }
        public void btnEdit_Click(object sender, DirectEventArgs e)
        {
            this.WindowEditor.Show();
        }
        public void btnDelete_Click(object sender, DirectEventArgs e) { }
        public void btnPrint_Click(object sender, DirectEventArgs e) { }

        /// <summary>
        /// Xuất Excel
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected void ToExcel(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();
            StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
            XmlNode xml = eSubmit.Xml;

            this.Response.Clear();
            this.Response.ContentType = "application/vnd.ms-excel";
            this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
            XslCompiledTransform xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath("~/Excel.xsl"));
            xtExcel.Transform(xml, null, this.Response.OutputStream);
            this.Response.End();
        }
        public void btnFind_Click(object sender, DirectEventArgs e) { }
        public void storeMain_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            BindingObject();
        }
    }
}
